<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link th:href="@{/assets/css/bootstrap.min.css}" rel="stylesheet" />
    <link rel="stylesheet" th:href="@{/css/style.css}"/>
    <link th:href="@{/assets/css/codemirror.css}" rel="stylesheet">
    <link rel="stylesheet" th:href="@{/assets/css/ace.min.css}" />
    <link rel="stylesheet" th:href="@{/font/css/font-awesome.min.css}" />
    <!--[if lte IE 8]>
    <link rel="stylesheet" href="assets/css/ace-ie.min.css" />
    <![endif]-->
    <script th:src="@{/js/jquery-1.7.2.min.js}"></script>
    <script th:src="@{/js/jquery.metadata.js}"></script>
    <script th:src="@{/js/jquery.validate.js}"></script>
    <script th:src="@{/js/jquery.validate.messages_cn.js}"></script>
    <script th:src="@{/assets/js/bootstrap.min.js}"></script>
    <script th:src="@{/assets/js/typeahead-bs2.min.js}"></script>
    <script th:src="@{/assets/js/jquery.dataTables.min.js}"></script>
    <script th:src="@{/assets/js/jquery.dataTables.bootstrap.js}"></script>
    <script th:src="@{/assets/layer/layer.js}" type="text/javascript" ></script>
    <script th:src="@{/assets/laydate/laydate.js}" type="text/javascript"></script>
    <title>管理权限</title>
    <style>
        .error{
            color: red;
        }
    </style>
</head>

<body>
<div class="margin clearfix">
    <div class="border clearfix">
       <span class="l_f">
        <a href="javascript:void(0)" id="administrator_add" class="btn btn-warning" title="新增角色"><i class="fa fa-plus"></i> 新增角色</a>
        <a href="javascript:void(0)" id="delete" class="btn btn-danger"><i class="fa fa-trash"></i> 批量删除</a>
       </span>
        <span class="r_f">共：<b th:text="${size}"></b>类</span>
    </div>
    <div class="compete_list">
        <table id="sample-table-1" class="table table-striped table-bordered table-hover">
            <thead>
            <tr>
                <th class="center"><label><input type="checkbox" id="all" onclick="allSelect()" class="ace"><span class="lbl"></span></label></th>
                <th>角色名称</th>
                <th>人数</th>
                <th>用户名称</th>
                <th class="hidden-480">描述</th>
                <th class="hidden-480">操作</th>
            </tr>
            </thead>
            <tbody>
            <tr th:each="role:${allrole}">
                <td class="center"><label><input type="checkbox" th:value="${role.rid}" name="flag" class="ace"><span class="lbl"></span></label></td>
                <td th:text="${role.name}"></td>
                <td th:text="${count.get(role.rid)}">0</td>
                <td class="hidden-480" th:text="${map.get(role.rid)}"></td>
                <td th:text="${role.desc}"></td>
                <td>
                                   <!--th:href="@{/role/jump(id=${role.rid})}"绑定功能-->
                    <span th:if="${role.rid eq 1}"><a title="编辑"  onclick="warning()"  class="btn btn-xs btn-info" ><i class="fa fa-edit bigger-120"></i></a></span>
                    <span th:if="${role.rid ne 1}"><a title="编辑" th:onclick="'updatePrivilege('+${role.rid}+')'"   class="btn btn-xs btn-info" ><i class="fa fa-edit bigger-120"></i></a></span>
                    <a title="删除" href="javascript:;"  th:onclick="'del(this,'+${role.rid}+')'" class="btn btn-xs btn-warning" ><i class="fa fa-trash  bigger-120"></i></a>
                </td>
            </tr>
            <!--<tr>
                <td class="center"><label><input type="checkbox" class="ace"><span class="lbl"></span></label></td>
                <td>普通管理员</td>
                <td>3</td>
                <td class="hidden-480">admin123 , 张小泉 ,克雷鲍</td>
                <td>拥有网站的系统大部分使用权限，没有权限管理功能。</td>
                <td>
                    <a title="编辑" onclick="Competence_modify('561')" href="javascript:;"  class="btn btn-xs btn-info" ><i class="fa fa-edit bigger-120"></i></a>
                    <a title="删除" href="javascript:;"  onclick="Competence_del(this,'2')" class="btn btn-xs btn-warning" ><i class="fa fa-trash  bigger-120"></i></a>
                </td>
            </tr>
            <tr>
                <td class="center"><label><input type="checkbox" class="ace"><span class="lbl"></span></label></td>
                <td>编辑管理员</td>
                <td>5</td>
                <td class="hidden-480">admin345,stysty,adminstyle,admin45678,admin123455</td>
                <td>拥有部分权限，主要进行编辑功能，无边界订单功能，权限分配功能。</td>
                <td>
                    <a title="编辑" onclick="Competence_modify('562')" href="javascript:;"  class="btn btn-xs btn-info" ><i class="fa fa-edit bigger-120"></i></a>
                    <a title="删除" href="javascript:;"  onclick="Competence_del(this,'3')" class="btn btn-xs btn-warning" ><i class="fa fa-trash  bigger-120"></i></a>
                </td>
            </tr>-->
            </tbody>
        </table>
    </div>

    <div id="add_administrator_style" class="add_menber" style="display:none">
        <form th:action="@{/role/addRole}" method="post" id="form-admin-add">
            <div class="form-group">
                <label class="form-label"><span class="c-red">*</span> 角色名：</label>
                <div class="formControls">
                    <input type="text" class="input-text" value="" placeholder="" id="name" name="name" datatype="*2-16" nullmsg="登录名不能为空">
                    <label class="error"></label>
                </div>
                <div class="col-4"> <span class="Validform_checktip"></span></div>
            </div>
            <div class="form-group">
                <label class="form-label"><span class="c-red">*</span>角色描述：</label>
                <div class="formControls">
                    <input type="text" class="input-text" value="" placeholder="" id="desc" name="desc" datatype="*2-16" nullmsg="真实名不能为空">
                    <label class="error"></label>
                </div>
                <div class="col-4"> <span class="Validform_checktip"></span></div>
            </div>


            <div>
                <input class="btn btn-primary radius" type="submit" id="Add_Administrator" value="&nbsp;&nbsp;提交&nbsp;&nbsp;"></div>
        </form>
    </div>

    <div id="update_administrator_style" class="add_menber" style="display:none">
        <form th:action="@{/role/addRole}" method="post" id="privilege-update">
            <input type="hidden" name="rid" value="">
        </form>
    </div>
</div>
<!--添加权限样式-->
<!-- <div id="Competence_add_style" style="display:none">
  <div class="Competence_add_style">
    <div class="form-group"><label class="col-sm-2 control-label no-padding-right" for="form-field-1"> 权限名称 </label>
      <div class="col-sm-9"><input type="text" id="form-field-1" placeholder=""  name="权限名称" class="col-xs-10 col-sm-5"></div>
   </div>
    <div class="form-group"><label class="col-sm-2 control-label no-padding-right" for="form-field-1"> 权限说明 </label>
      <div class="col-sm-9"><textarea name="权限说明" class="form-control" id="form_textarea" placeholder="" onkeyup="checkLength(this);"></textarea><span class="wordage">剩余字数：<span id="sy" style="color:Red;">200</span>字</span></div>
   </div>
  </div>
 </div>-->
</body>

<script type="text/javascript" th:inline="none">

    function updatePrivilege(id) {
        $('input[type="hidden"]').val('');
        $('input[type="hidden"]').val(id);
        $('input[name="flag1"]').removeAttr('checked');
        $.ajax({
            url:'/role/updatePrivilege',
            type:'get',
            dataType:'json',
            data:'id='+id,
            success:function (data) {
                if (data==null || data==""){
                    layer.open({
                        type: 1,
                        title:'修改权限',
                        area: ['700px',''],
                        shadeClose: false,
                        content: $('#update_administrator_style'),
                    });
                }else{
                    $.each(data,function (i,item) {
                        $('input[id="'+item.pid+'"]').attr('checked',true);
                    })
                    layer.open({
                        type: 1,
                        title:'修改权限',
                        area: ['700px',''],
                        shadeClose: false,
                        content: $('#update_administrator_style'),
                    });
                }
            }
        })
    }

    $(function () {
        $.ajax({
            url:'/role/default',
            type:'get',
            dataType:'json',
            data:'id='+1,
            success:function (data) {
                $('#privilege-update').append('<h2>现有以下模块权限，请修改！！！</h2>')
                $.each(data,function (i,item) {
                    $('#privilege-update').append('<div class="form-group"><label class="form-label"><span class="c-red">*</span></label>' +
                        '<div class="formControls">' +
                        '<a><input name="flag1" id="'+item.pid+'" value="'+item.pid+'" type="checkbox">'+item.name+'</a>' +
                        '<label class="error"></label>' +
                        '</div>' +
                        '<div class="col-4"> <span class="Validform_checktip"></span></div>' +
                        '</div>');
                })
                $('#privilege-update').append('<p>' +
                    '<span class="btn btn-primary radius" type="submit" onclick="updateEnd()" id="Add_Administrator" value="&nbsp;&nbsp;&nbsp;&nbsp;">提交</span>');
            }
        })
    })

    function  updateEnd() {
        var obj = document.getElementsByName("flag1");/*拿到所有name=flag的对象*/
        var result = $("input[name='flag1']").is(':checked');
        if (result==false) {
            return alert('请点击复选框在进行操作');
        }
        var s= "";
        for (var i = 0; i < obj.length; i++) {
            if (obj[i].checked) {
                s +=obj[i].value+",";
            }
        };

        var id = $('input[type="hidden"]').val();
        $.ajax({
            url:'/role/updateEnd',
            type:'get',
            dataType:'json',
            data:{ids:s,rid:id},
            success:function (data) {
                if (data>=1){
                    alert('修改成功!!!');
                    window.location.reload();
                }
            }
        })
    }

    function warning(){
        alert('您不允许修改超级管理员！！！');
    }

    function allSelect(){
        var all = document.getElementById("all");
        var single = document.getElementsByName("flag");
        for (let i = 0; i < single.length; i++) {
            single[i].checked = all.checked;
        }
    }
    /*添加权限*/
    /* $('#Competence_add').on('click', function(){
         layer.open({
            type: 1,
            title: '添加权限',
            maxmin: true,
            shadeClose: false,
            area : ['800px' , ''],
            content:$('#Competence_add_style'),
            btn:['提交','取消'],
            yes:function(index,layero){
             var num=0;
             var str="";
         $(".col-sm-9 input[type$='text'],#form_textarea").each(function(n){
              if($(this).val()=="")
              {

                   layer.alert(str+=""+$(this).attr("name")+"不能为空！\r\n",{
                    title: '提示框',
                    icon:0,
              });
                num++;
                return false;
              }
             });
              if(num>0){  return false;}
              else{
                  layer.alert('添加成功！',{
                   title: '提示框',
                icon:1,
                  });
                   layer.close(index);
              }
            }
        });
     });*/
    /*权限-删除*/
    function del(obj,id){
        layer.confirm('确认要删除吗？',function(index){
            $.ajax({
                url:'/role/del',
                type: 'get',
                dataType:'json',
                data:'id='+id,
                success:function (data) {
                    if (data==1){
                        $(obj).parents("tr").remove();
                        layer.msg('已删除!',{icon:1,time:1000});
                    }
                }
            })
        });
    }

    /*字数限制*/
    function checkLength(which) {
        var maxChars = 200; //
        if(which.value.length > maxChars){
            layer.open({
                icon:2,
                title:'提示框',
                content:'您出入的字数超多限制!',
            });
            // 超过限制的字数了就将 文本框中的内容按规定的字数 截取
            which.value = which.value.substring(0,maxChars);
            return false;
        }else{
            var curr = maxChars - which.value.length; //250 减去 当前输入的
            document.getElementById("sy").innerHTML = curr.toString();
            return true;
        }
    };
    //面包屑返回值
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.iframeAuto(index);
    $('.Order_form ,#Competence_add').on('click', function(){
        var cname = $(this).attr("title");
        var cnames = parent.$('.Current_page').html();
        var herf = parent.$("#iframe").attr("src");
        parent.$('#parentIframe span').html(cname);
        parent.$('#parentIframe').css("display","inline-block");
        parent.$('.Current_page').attr("name",herf).css({"color":"#4c8fbd","cursor":"pointer"});
        //parent.$('.Current_page').html("<a href='javascript:void(0)' name="+herf+">" + cnames + "</a>");
        parent.layer.close(index);

    });

    /*批量删除角色*/
    $('#delete').on('click', function() {
        var obj = document.getElementsByName("flag");/*拿到所有name=flag的对象*/
        var result = $("input[type='checkbox']").is(':checked');
        if (result==false) {
            return alert('请点击复选框在进行操作');
        }
        var s= "";
        for (var i = 0; i < obj.length; i++) {
            if (obj[i].checked) {
                s +=obj[i].value+",";
            }
        };
        $.ajax({
            url:'/role/delSome',
            type:'get',
            dataType:'json',
            data:'ids='+s,
            success:function (data) {
                if (data>=1){
                    window.location.reload();
                }
            }
        })
    })


    /*添加管理员*/
    $('#administrator_add').on('click', function(){
        layer.open({
            type: 1,
            title:'新增角色',
            area: ['700px',''],
            shadeClose: false,
            content: $('#add_administrator_style'),
        });
    })
    //表单验证提交
    $(function(){
        $('#form-admin-add').validate({
            rules:{
                name:{required: true,rangelength:[2,10]},
                desc:{required: true}
            },
            messages:{
                name:{
                    required:"角色名不能为空",
                    rangelength: "长度在2-10之间",
                },
                desc: {
                    required:"描述不能为空",
                }
            }
        })
    })
</script>
</html>